/***************************************************************************
 创建者：　华磊
 开始时间：               : 2020.11.16
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：
 华磊 2020.12        整合
 华磊 2016           补充移动到点功能
 华磊\华磊          新指令关键词提取策略整合
 华磊 2017.2.8       移动到点补充状态判断

 ***************************************************************************
 *   程序指令视窗测试用代理类
 *                                                                         *
 ***************************************************************************/
#ifndef PROGRAMDELEGATE_H
#define PROGRAMDELEGATE_H

#include "programdelegate_global.h"
#include "../programView/programview.h"
#include "../ProgramSet/motioninstruction.h"
#include "programcheck.h"
#include <QObject>
#include <QRegExp>
#include "line.h"


class ProgramLanguageTranslate;
class ProgramRegExp;


/**
 * @brief The ProgramDelegate class 窗体业务逻辑类
 */
class PROGRAMDELEGATESHARED_EXPORT ProgramDelegate :public QObject
{

    Q_OBJECT

public:
    ProgramDelegate(CommunicateNormalDelegate *communicateNormalDelegateIn,
                    ProgramLanguageTranslate *programLanguageTranslateIn,
                    ProgramView *programViewIn, GlobalData *globalDataIn,
                    MoveOperation *moveOperationIn, GlobalData *ioDataIn,
                    MapView *mapViewIn,QObject *parent = 0);
    ~ProgramDelegate();



    /**
     * @brief hideAllInstructionView    隐藏所有指令窗口
     */
    void hideAllInstructionView();


/**
     * @brief instructionIsArc　判断是否为C指令
     * @return
     */
    bool instructionIsArc();

    int setMusicFileList(QVector <QString> musicFileListIn);




private:
    /**
     * @brief initialView　初始化界面成员变量
     */
    void initialView();


    void initConnect();
    void initialConnectButtonFunc();



public slots:
    void setCoupleControl_slot(int followAxisId,bool isEnable);
    void showWeldPropetyViewErrorMsg(QString msg);
    void timeOrderWork();
    void initialMusicFileList();
    void initialMusicFileListStart(int result, QString note);
    void setMusicFileList(std::vector<std::string> musicFileListIn);
    /**
     * @brief openFileEditSlot      收到proFiled通知打开了文件,显示该文件指令
     */
    void openFileEditSlot();

    /**
     * @brief moveUpInstructionSlot 指令上移响应槽函数
     * @param currentLineIndex      当前指令行号
     * @return
     */
    void moveUpInstructionSlot(int currentLineIndex);
    /**
     * @brief moveDownInstructionSlot   指令下移响应槽函数
     * @param currentLineIndex      当前指令行号
     * @return
     */
    void moveDownInstructionSlot(int currentLineIndex);
    /**
     * @brief deleteInstructionSlot 删除指令响应槽函数
     * @param currentLineIndex      当前指令行号
     * @return
     */
    void deleteInstructionSlot(int currentLineIndex);
    /**
     * @brief noteInstructionSlot   指令注释响应槽函数
     * @param currentLineIndex      当前指令行号
     * @return
     */
    void noteInstructionSlot(int currentLineIndex);
    /**
     * @brief recordPointSlot       载入点响应槽函数
     * @param currentLineIndex      当期指令行号
     * @return
     */
    int recordPointSlot(int currentLineIndex);          //根据便捷界面的指令
    void recordPointSlotByPointNum(int pointNumber);    //根据点号
    void recordPointInsert(int pointNumber);            //根据插入运动指令
    /**
     * @brief showPointSlot         点查看响应槽函数
     * @param currentRow            当前行内容
     * @return
     */
    void showPointSlot(QString currentRow);
    /**
     * @brief jToPointSlot          关节到点响应槽函数
     * @param currentLineIndex      当前行号
     * @return
     */
    void jToPointSlot(int currentLineIndex);
    /**
     * @brief lToPointSlot          直线到点响应槽函数
     * @param currentLineIndex      当前行号
     * @return
     */
    void lToPointSlot(int currentLineIndex);


    void pointViewjMoveSlot(int pointNumber);

    void pointViewlMoveSlot(int pointNumber);

    /**
     * @brief stopMoveToPointSlot   停止移动到点
     */
    void stopMoveToPointSlot();


    //**************************
    /**
     * @brief saveFileSlot      保存文件响应槽函数
     * @param fileName          文件名
     * @param fileInformation   文件指令内容
     * @return
     */
    //int saveFileSlot(QString fileName, QStringList fileInformation);
    void saveFileSlot();

    /**
     * @brief menuViewShowOrHideSlot    指令视窗显示与否响应槽函数
     * @param menuViewShowFlag          指令视窗显示与否标识
     */
    void menuViewShowOrHideSlot(int menuViewShowFlag);
    /**
     * @brief menuChangeSlot            指令视窗显示窗口变化响应槽函数
     * @param currentMenuIndex          当前显示的指令窗口
     */
    //void menuChangeSlot(int currentMenuIndex);
    void menuChangeSlot();

    //**************************
    //收到来自rightFun的移动点信号
    void rightFunMoveToPointSlot(int type);
    void rightFunMoveStopSlot();

    //**************************
    //点查看相关
    /**
     * @brief changePointToDegSlot      转换成关节点
     * @param pointNum
     */
    void changePointToDegSlot(int pointNum);
    /**
     * @brief changePointToWorldSlot    转换成笛卡尔点
     * @param pointNum
     */
    void changePointToWorldSlot(int pointNum);
    /**
     * @brief recvChangePointAndSave    接收来自comm通信的返回转换点,并且载入该点
     */
    void recvChangePointAndSave(PointPro recvPoint);

    //**************************
    //常用指令视窗各指令响应槽函数
    void oftenForwardPageSlot();
    void oftenNextPageSlot();
    void jInstructionSlot();
    void lInstructionSlot();
    void axisMoveInstruction_Slot();
    void arcInstructionSlot();
    void labelInstructionSlot();
    void jumpInstructionSlot();
    void conditionInstructionSlot();
    void waitInstructionSlot();

    //全部指令视窗各指令响应槽函数
    void allForwardPageSlot();
    void allNextPageSlot();
    void openMoveMenuSlot();
    void openPalletMenuSlot();
    void openFlowMenuSlot();
    void openRegisterMenuSlot();
    void openIoMenuSlot();
    void openOtherMenuSlot();
    void openStampingMenuSlot();
    void openWeldMenuSlot();

    void openGuidanceMenuSlot();
    void openModbusMenuSlot();
    void openTcpCommunicationSlot();

    // 导航指令视窗各指令响应槽函数
    void guidanceMoveMagSlot();
    void guidanceMoveFreeSlot();
    void guidanceForwardPageSlot();
    void guidanceNextPageSlot();
//    void guidanceSetOdometrySlot();

    // 总线指令窗口响应槽函数
    void modbusStartSlot();
    void modbusReadSlot();
    void modbusWriteSlot();
    void modbusForwardPageSlot();
    void modbusNextPageSlot();

    //　重置里程计指令窗口响应槽函数
    void setOdomResetSlot();

    // TCP 以太网通信指令菜单窗口响应槽函数
    void tcpStartSlot();
    void tcpReadSlot();
    void tcpWriteSlot();
    void tcpForwardPageSlot();
    void tcpNextPageSlot();

    //运动指令视窗各指令响应槽函数
    void moveForwardPageSlot();
    void moveNextPageSlot();

    //码垛指令视窗各指令响应槽函数
    void palletForwardPageSlot();
    void palletNextPageSlot();
    void palletHeadInstructionSlot();//插入一个头
    void palletTailInstructionSlot();
    void approachPointInstructionSlot();
    void fleePointInstructionSlot();
    void stackPointInstructionSlot();

    //流程指令视窗各指令响应槽函数
    void flowForwardPageSlot();
    void flowNextPageSlot();
    void selectInstructionSlot();
    void loopInstructionSlot();
    void callInstructionSlot();
    void pauseInstructionSlot();
    void createTaskInstructionSlot();
    void abortInstructionSlot();
    void endInstructionSlot();

    //寄存器指令视窗各指令响应槽函数
    void registerForwardPageSlot();
    void registerNextPageSlot();
    void rInstructionSlot();
    void pRInstructionSlot();
    void pLInstructionSlot();
    void timerInstructionSlot();

    //端口指令视窗各指令响应槽函数
    void ioForwardPageSlot();
    void ioNextPageSlot();
    void numberInstructionSlot();
    void analogInstructionSlot();
    void systemInstructionSlot();

    //其它指令视窗各指令响应槽函数
    void otherForwardPageSlot();
    void otherNextPageSlot();
    void toolCoordinateInstructionSlot();
    void userCoordinateInstructionSlot();
    void toolCompensateInstructionSlot();
    void positionCompensateInstructionSlot();
    void rateInstructionSlot();
    void messageInstructionSlot();
    void alarmInstructionSlot();
    void rsrInstructionSlot();
    void visionInstructionSlot();//视觉

    // 其他指令下一页视窗各指令响应槽函数
    void otherNextMenuForwardPageSlot();
    void otherNextMenuNextPageSlot();
    void addloadmapinstructionSlot();
    void addCheckBatteryInstructionSlot();  //电池电量检测指令
    void addWaitCommandInstructionSlot();   //等待命令
    void addRobotMoveInstruction_Slot();    // 机械手运动指令
    void addRollerInstruction_slot();       // 滚筒指令
    void musicInstructionSlot();
    void chargingInstructionSlot();
    void liftMechanismInstructionSlot();

    void showLaserCameraMenu_slot();
    void laserCameraMenuForwardPage_slot();



    //冲压指令视窗各指令响应槽函数
    void stampForwardPageSlot();
    void stampNextPageSlot();
    void getInstructionSlot();//取
    void getWaitInstructionSlot();
    void getAboveInstructionSlot();
    void getEndInstructionSlot();
    void putInstructionSlot();//放
    void putWaitInstructionSlot();
    void putAboveInstructionSlot();
    void putEndInstructionSlot();
    //拓展新指令增加代码20191122hualei
    void addWeldStartInstruction_slot();
    //新指令20191130 ZHUHT
    void addWeldEndInstruction_slot();
    void addWeaveInstruction_slot();
    void addWeaveEndInstruction_slot();
    void addTrackWeldEndInstruction_slot();
    void addTrackWeldTastInstruction_slot();
    void addWatchDIInstruction_slot();

    void addLaserCameraTrackIns_slot();
    void addLaserCameraFetchPointIns_slot();
    void addLaserCameraControlIns_slot();
    void addLaserScan_slot();
    void addLaserScanEnd_slot();
    void addBufferMove_slot();

    /**
     * @brief getCurrentLineInstructionSlot 获取选中行指令内容槽函数
     * @param index　选中行
     */
    void getCurrentLineInstructionSlot(int index);
    /**
     * @brief saveInstructionSlot           保存指令槽函数
     * @param instruction                   需保存指令
     */
    void saveInstructionSlot(QString instruction);

    /**
     * @brief openInstructionEditViewSlot   打开指令编辑界面槽函数
     */
    void openInstructionEditViewSlot();

    /**
     * @brief changeLanguageSlot            修改语言槽函数
     * @param language
     */
    void changeLanguageSlot(int language);

    /**
     * @brief changeEditLanguageSlot        修改编辑语言槽函数
     * @param language
     */
    void changeEditLanguageSlot(int language);

    /**
     * @brief showFileInfoChineseSlot       程序中文显示
     * @return
     */
    void showFileInfoChineseSlot();

    /**
     * @brief setIsSaveState                改变issave状态
     * @param state
     */
    void setIsSaveState(bool state);

    /**
     * @brief ftpFinishedGetFile            TFP 上传保存的程序文件后控制器打开文件
     * @param result
     * @param message
     */
    void ftpFinishedPutFile(bool result, const QString& message);

    /**
     * @brief savecurrentinstructionSlot        点击指令列表按钮时更新保存指令数据
     */
    void saveCurrentinstruction_Slot();

    void setDefaultJMoveSpeed_slot(double speed);
    void setDefaultLMoveSpeed_slot(double speed,QString defaultUniteType);


signals:
    void openUserProgramFile_signal(QString fleName);

    /**
     * @brief showPointInMap                绘制目标点显示在地图界面上
     */
    void showPointInMap();

    /**
     * @brief msgBoxSignal                  通知 打开消息框
     */
    void msgBoxSignal(int type,QString title,QString text);

    /**
     * @brief msgBoxGsoapWrite_Signal       gsoap文件写入消息框
     * @param type
     * @param title
     * @param text
     */
    void msgBoxGsoapWrite_Signal(int type,QString title,QString text);

    void saveFileAndUpdateCtrlSignal(QString fileName,QString md5);

    /**
     * @brief moveToPointSignal             移动到点 信号
     * @param movePoint
     */
//    void moveToPointSignal(MoveParameter movePoint);

    /**
     * @brief moveToPointSignal             停止移动到点 信号
     * @param movePoint
     */
//    void stopMoveToPointSignal();

    /**
     * @brief changePointToDegCommSignal    转换 关节点
     * @param tmpPoint
     */
    void changePointToDegCommSignal(PointPro tmpPoint);
    /**
     * @brief changePointToWorldCommSignal  转换 笛卡尔点
     * @param tmpPoint
     */
    void changePointToWorldCommSignal(PointPro tmpPoint);

    void setPrSignal(int number,PointPro tmpPoint);

    /**
     * @brief saveMoveFreeInstruction       更新保存运动指令
     */
    void saveMotionInstruction_Signal();

    /**
     * @brief saveIfInstruction_Signal      更新保存条件指令
     */
    void saveIfInstruction_Signal();

    /**
     * @brief saveLblJmpInstruction_Signal  更新保存标签跳转指令
     */
    void saveLblJmpInstruction_Signal();

    /**
     * @brief saveWaitInstruction_Signal      更新保存等待指令
     */
    void saveWaitInstruction_Signal();

    /**
     * @brief saveForInstruction_Signal             更新保存循环指令
     */
    void saveForInstruction_Signal();

    /**
     * @brief saveRegisterInstruction_Signal        更新保存寄存器指令
     */
    void saveRegisterInstruction_Signal();

    /**
     * @brief saveSelectInstruction_Signal          更新保存选择指令
     */
    void saveSelectInstruction_Signal();

    /**
     * @brief saveSkipInstruction_Signal            更新保存跳转指令
     */
    void saveSkipInstruction_Signal();

    /**
     * @brief saveOutputInstruction_Signal          更新保存输出指令
     */
    void saveOutputInstruction_Signal();

    /**
     * @brief saveCoordinateInstruction_Signal      更新保存坐标指令
     */
    void saveCoordinateInstruction_Signal();

    /**
     * @brief saveOtherInstruction_Signal           更新保存其他指令
     */
    void saveOtherInstruction_Signal();

    /**
     * @brief savePalletInstruction_Signal          更新保存码垛指令
     */
    void savePalletInstruction_Signal();

    /**
     * @brief savePalletMotionInstruction_Signal        更新保存码垛运动指令
     */
    void savePalletMotionInstruction_Signal();

    /**
     * @brief saveVisionInstruction_Signal              更新保存版本指令
     */
    void saveVisionInstruction_Signal();

    /**
     * @brief saveEndInstruction_Signal                 更新保存结束指令
     */
  //void saveEndInstruction_Signal();

    /**
     * @brief saveMoveMagInstruction_Signal             更新保存磁导航指令
     */
    void saveMoveMagInstruction_Signal();

    /**
     * @brief saveModbusStartInstruction_Signal         更新保存Modbus总线创建指令
     */
    void saveModbusStartInstruction_Signal();

    /**
     * @brief saveModbusReadInstruction_Signal          更新保存Modbus总线读取指令
     */
    void saveModbusReadInstruction_Signal();

    /**
     * @brief saveModbusWriteInstruction_Signal         更新保存Modbus总线发送指令
     */
    void saveModbusWriteInstruction_Signal();

    /**
     * @brief saveSetOdomInstruction_Signal            更新保存重置里程计指令
     */
    void saveSetOdomInstruction_Signal();

    /**
     * @brief saveTcpStartInstruction_Signal           更新保存tcp以太网创建指令
     */
    void saveTcpStartInstruction_Signal();

    /**
     * @brief saveTcpReadInstruction_Signal             更新保存tcp以太网接收指令
     */
    void saveTcpReadInstruction_Signal();

    /**
     * @brief saveTcpWriteInstruction_Signal            更新保存tcp以太网发送指令
     */
    void saveTcpWriteInstruction_Signal();

    /**
     * @brief saveChargingInstruction_Signal            更新保存充电指令
     */
    void saveChargingInstruction_Signal();

    /**
     * @brief saveMusicInstruction_Signal               更新保存音乐指令
     */
    void saveMusicInstruction_Signal();

    /**
     * @brief saveLiftInstruction_Signal                更新保存升降机构指令
     */
    void saveLiftInstruction_Signal();

    /**
     * @brief saveLoadMapInstruction_Signal             更新保存加载地图指令
     */
    void saveLoadMapInstruction_Signal();

    /**
     * @brief saveMoveFreeInstruction                   更新保存自由导航指令
     */
    void saveMoveFreeInstruction_Signal();

    /**
     * @brief saveCheckBatteryInstruction_Signal                   更新保存电量检测指令
     */
    void saveCheckBatteryInstruction_Signal();
    /**
     * @brief saveCheckBatteryInstruction_Signal                   更新等待命令指令
     */
    void saveWaitCommandInstruction_Signal();

    /**
     * @brief saveRobotMoveInstruction_Signal                   机械手控制指令
     */
    void saveRobotMoveInstruction_Signal();
    void saveRollerInstruction_Signal();
    void saveAxisMoveInstruction_Signal();
    //拓展新指令增加代码20191122hualei
    void saveWeldStartInsTruction_signal();
    //新指令20191130 ZHUHT
    void saveWeldEndInsTruction_signal();
    void saveWeaveInsTruction_signal();
    void saveWeaveEndInstruction_signal();
    void saveTrackWeldEndInstruction_signal();
    void saveTrackWeldTastInstruction_signal();
    void saveWatchDIInstruction_signal();
    void saveLaserCameraControlIns_signal();
    void saveLaserCameraTrackIns_signal();
    void saveLaserCameraFetchPointIns_signal();
    void saveBufferMoveIns_signal();

private:
    /**
     * @brief moveToPointSlot       移动到点(行号)
     * @param moveType
     * @param currentLineIndex
     */
    void moveToPointSlot(int moveType , int currentLineIndex);

    /**
     * @brief moveToPointSlot       移动到点(点号)
     * @param moveType
     * @param currentLineIndex
     */
    void moveToPointSlotByNum(int moveType , int pointNumber);

   int transferProgramTextToStructData();

    //拓展新指令增加代码20191122hualei
   int patWeldStart(QString programStr);
   //新指令20191130 ZHUHT
   int patWeldEnd(QString programStr);
   int patWeave(QString programStr);
   int patWeaveEnd(QString programStr);
   int patTrackWeldEnd(QString programStr);
   int patTrackWeldTast(QString programStr);
   int patWatchDI(QString programStr);
   int patLaserCameraControl(QString programStr);
   int patLaserCameraTrack(QString programStr);
   int patLaserCameraFetchPoint(QString programStr);
   int patBufferMove(QString programStr);


   int patCheckBattery(QString programStr);
   int patAxisMove(QString programStr);
   int patMotion(QString programStr, QString forwardLineString);
   int patIf(QString programStr, QString forwardLineString);
   int patEndCallRunInstruction(QString programStr);
   int patLabelInstruction(QString programStr);
   int patJmpInstruction(QString programStr);
   int patWait(QString programStr);
   int patFor(QString programStr);
   int patRegisterInstruction(QString programStr);
   int patPalletInstruction(QString programStr);

   int patPunchInstruction(QString programStr);
   int patSelectInstruction(QString programStr);
   int patSkipInstruction(QString programStr);
   int patDoInstruction(QString programStr);
   int patCoordinateInstruction(QString programStr);
   int patOther1Instruction(QString programStr);
   int patVisionRunInstruction(QString programStr);
   int patVisionTriggerInstruction(QString programStr);
   int patVisionGetDataInstruction(QString programStr);
   int patVisionClearDataInstruction(QString programStr);
   int patTrackStartInstruction(QString programStr);
   int patTrackEndInstruction(QString programStr);
   int patTrackGetDataInstruction(QString programStr);


    /**
     * @brief analysisMusicInstruction          Analysis The MUSIC instruction
     */
    void analysisMusicInstruction();

    /**
     * @brief analysisChargingInstruction       CHARGING充电指令
     */
    void analysisChargingInstruction();

    /**
     * @brief analysisLiftMechanismInstruction  LIFT 升降机构指令
     */
    void analysisLiftMechanismInstruction();

    /**
     * @brief analysisTcpWriteInstruction       TCP_WRITE tcp以太网发送数据指令
     */
    void analysisTcpWriteInstruction();

    /**
     * @brief analysisTcpStartInstruction       TCP_START tcp以太网创建指令
     */
    void analysisTcpStartInstruction();

    /**
     * @brief analysisTcpReadInstruction        TCP_READ tcp以太网接收数据指令
     */
    void analysisTcpReadInstruction();

    /**
     * @brief analysisMoveFreeInstruction       MOVE_FREE自由导航指令
     */
    void analysisMoveFreeInstruction();
    void analysisPrRegisterInstruction();

    /**
     * @brief analysisMoveMagInstruction        MOVE_MAG 磁导航指令
     */
    void analysisMoveMagInstruction();

    /**
     * @brief analysisModbusStartInstruction    MODBUS_START Modbus起始指令
     */
    void analysisModbusStartInstruction();

    /**
     * @brief analysisModbusReadInstruction     MODBUS_READ Modbus读指令
     */
    void analysisModbusReadInstruction(QString programStr);

    /**
     * @brief analysisModbusWriteInstruction    MODBUS_WRITE Modbus写指令
     */
    void analysisModbusWriteInstruction(QString programStr);

    /**
     * @brief analysisSetOdomInstruction        SET_ODOM重置里程计指令
     */
    void analysisSetOdomInstruction();

    /**
     * @brief analysisLoadMapInstruction        LOAD_MAP加载地图指令
     */
    void analysisLoadMapInstruction();


    /**
     * @brief analysisWaitCommand              WaitCommand 等待命令
     */
    void analysisWaitCommand();

    /**
     * @brief analysisRobotMove                 ROBOT_MOVE机械手控制
     */
    void analysisRobotMove();

    /**
     * @brief analysisRoller                    ROLLER　滚筒指令
     */
    void analysisRoller();

private:
private:
    // 地图视图类，提供获取地图点位
    MapView *mapView;
    ProgramCheck programCheck;


    /**
     * @brief programLanguageTranslate      程序语言翻译类
     */
    ProgramLanguageTranslate *programLanguageTranslate;

    /**
     * @brief programView           程序指令视窗类指针
     */
    ProgramView *programView;

    /**
     * @brief globalData            指针 公共数据
     */
    GlobalData *globalData;
    GlobalData *ioData;

    /**
     * @brief moveOperation         运动操作指针
     */
    MoveOperation *moveOperation;

    /**
     * @brief instructionListViewFlag 指令列表:0 常用指令 1 全部指令
     */
    int instructionListViewFlag;

    /**
     * @brief currentProgram        当前程序英文显示
     */
    QStringList currentProgram;
    /**
     * @brief programLocation       当前程序路径
     */
    QString programLocation;

    /**
     * @brief maxUsedPointIndex     已经使用的最大的点编号
     */
    int maxUsedPointIndex;

    /**
     * @brief getFileAddress        文件浏览器 路径
     */
    QString getFileAddress;

    QTimer *timerOrder;


    QTranslator translator;

    bool ftpGetSaveProgramFileFlag;

    CommunicateNormalDelegate *communicateNormalDelegate;
    ProgramRegExp* programRegExp;

    double defaultJMoveSpeed;
    double defaultLMoveSpeed;
    QString defaultUniteType;
};

#endif // PROGRAMDELEGATE_H
